﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using QLCONGNOLIBS.Ultils;
using QLCONGNOLIBS.Securities;
using QLCONGNOLIBS.Models.EntityFW;

namespace QLCONGNOLIBS.Models
{
    public class CustomerModels
    {
        //1. Datafield
        private SqlConnection m_objConnection;                
        private SecurityEntity m_objSecurity;
        private BUltils m_objBaseUltils;

        //2. Khai báo các thủ tục sẽ sử dụng
        private const string GET_CUSTOMERS = "SP_GET_CUSTOMERS";
        private const string SP_IN_UP_CUSTOMERS = "SP_INSERT_UPDATE_CUSTOMER";

        //3. Hàm khởi dựng - Constructors
        public CustomerModels(SqlConnection objConnection, SecurityEntity objSecurity)
        { 
            //Thiết lập thông tin kết nối cho datafield
            m_objConnection = objConnection;
            m_objSecurity = objSecurity;
        }

        public CustomerModels(string connectString)
        {
            //Khởi tạo kết nối với ConnectionString
            m_objConnection = new SqlConnection();
            if (m_objConnection.State == ConnectionState.Closed)
            {
                m_objConnection = ConnectionMans.OpenConnection(connectString);
            }
        }

        //4. Thực thi và nhận về dữ liệu là DataTable"        
        /// <summary>
        /// Hiển thị danh sách học viên
        /// </summary>
        /// <returns></returns>
        public DataTable GetElements()
        {
            DataTable objTable;            
            try
            {
                SqlParameter[] param = { };
                m_objBaseUltils = new BUltils(m_objConnection);
                objTable = m_objBaseUltils.ExecProcReturnTable(GET_CUSTOMERS, param);                               
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return objTable;
        }

        /// <summary>
        /// Nghiệp vụ thêm hoặc cập nhật thông tin khách hàng
        /// </summary>
        /// <param name="objCusEntity"></param>
        /// <param name="shOptions">shOptions = 0: Thêm & shOptions = 1: Cập nhật</param>
        /// <returns></returns>
        public bool Insert_Update_Element(ICustomer objCusEntity, short shOptions)
        {
            try
            {               
                m_objBaseUltils = new BUltils(m_objConnection);
                SqlParameter[] objParam = new SqlParameter[18];
                objParam[0] = m_objBaseUltils.CreateParammeter("@Flag", shOptions);
                objParam[1] = m_objBaseUltils.CreateParammeter("@CustomerID", objCusEntity.CustomerID);
                objParam[2] = m_objBaseUltils.CreateParammeter("@CustomerName", objCusEntity.CustomerName);
                objParam[3] = m_objBaseUltils.CreateParammeter("@CompanyName", objCusEntity.CompanyName);
                objParam[4] = m_objBaseUltils.CreateParammeter("@Address", objCusEntity.Address);
                objParam[5] = m_objBaseUltils.CreateParammeter("@PhoneNo", objCusEntity.PhoneNo);
                objParam[6] = m_objBaseUltils.CreateParammeter("@Email", objCusEntity.Email);
                objParam[7] = m_objBaseUltils.CreateParammeter("@CreditLine", objCusEntity.CreditLine);
                objParam[8] = m_objBaseUltils.CreateParammeter("@Due", objCusEntity.Due);
                objParam[9] = m_objBaseUltils.CreateParammeter("@Gov", objCusEntity.Gov);
                objParam[10] = m_objBaseUltils.CreateParammeter("@CityID", objCusEntity.CityID);
                objParam[11] = m_objBaseUltils.CreateParammeter("@Employee_ID", objCusEntity.Employee_ID);
                objParam[12] = m_objBaseUltils.CreateParammeter("@IsActive", objCusEntity.IsActive);
                objParam[13] = m_objBaseUltils.CreateParammeter("@IsDeleted", objCusEntity.IsDeleted);
                objParam[14] = m_objBaseUltils.CreateParammeter("@Fax", objCusEntity.Fax);
                objParam[15] = m_objBaseUltils.CreateParammeter("@BankAccount", objCusEntity.BankAccount);
                objParam[16] = m_objBaseUltils.CreateParammeter("@BankName", objCusEntity.BankName);
                objParam[17] = m_objBaseUltils.CreateParammeter("@Note", objCusEntity.Note);
                m_objBaseUltils.ExecProcNoReturn(SP_IN_UP_CUSTOMERS, objParam);
            }    
            catch (Exception ex)
            {
                throw ex;
            }            
            return true;
        }
    }
}
